package buzzerproxy.bprot;

import java.util.HashMap;

import buzzerproxy.handler.QuestionInterface;

/**
 * @author M. Friedeboldt
 */
public class SessionManager {

    private HashMap<Long, Session> map;

    private QuestionInterface      questionInterface;

    /**
     * @param questionInterface
     */
    public SessionManager(QuestionInterface questionInterface) {
        map = new HashMap<Long, Session>();
        this.questionInterface = questionInterface;
    }

    /**
     * @return
     */
    synchronized public long createNewSession() {
        Long sessionId = System.currentTimeMillis();
        while ( map.containsKey( sessionId ) )
            sessionId = System.currentTimeMillis();

        map.put( sessionId, new Session( questionInterface ) );
        return sessionId;
    }

    /**
     * @param sessionId
     * @return
     */
    synchronized public Session getSession(long sessionId) {
        return map.get( new Long( sessionId ) );
    }

    /**
     * @param sessionId
     */
    synchronized public void removeSession(Long sessionId) {
        map.remove( new Long( sessionId ) );
    }
}